package com.lipuwater.agent.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ismartlib.ui.view.checkbox.CheckBox;
import com.ismartlib.utils.DisplayUtil;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseActivity;
import com.lipuwater.agent.ble.control.DeviceResetFilterControl;
import com.lipuwater.agent.http.bean.after.DevFilterInfoBean;
import com.lipuwater.agent.ui.view.TabLayoutView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 更换滤芯
 * 这里如果继承BaseLayoutActivity的话，会导致自定义view的onDraw执行两次
 */
public class ReplaceFilterActivity extends BaseActivity {


    @Bind(R.id.tabLayoutView)
    TabLayoutView tabLayoutView;
    @Bind(R.id.hr_scrollview)
    HorizontalScrollView hrScrollview;
    @Bind(R.id.checkBox1)
    CheckBox checkBox1;
    @Bind(R.id.checkBox2)
    CheckBox checkBox2;
    @Bind(R.id.checkBox3)
    CheckBox checkBox3;
    @Bind(R.id.checkBox4)
    CheckBox checkBox4;
    @Bind(R.id.text_filter1)
    TextView textFilter1;
    @Bind(R.id.text_filter2)
    TextView textFilter2;
    @Bind(R.id.text_filter3)
    TextView textFilter3;
    @Bind(R.id.text_filter4)
    TextView textFilter4;
    @Bind(R.id.layout_content)
    LinearLayout layoutContent;
    @Bind(R.id.layout_nodata)
    LinearLayout layoutNodata;
    @Bind(R.id.layout_loading)
    LinearLayout layoutLoading;
    @Bind(R.id.nodata_image)
    ImageView nodataImage;
    @Bind(R.id.nodata_text)
    TextView nodataText;
    private int ScreenWidth;

    private int mPosition = 0;
    private int mLevel = 0;
    private int mA1FilterDay, mA2FilterDay, mA3FilterDay, mA4FilterDay;//四个滤芯分别可用天数
    private int minReplaceDay = 10;//最小重置天数

    private String mMac, deviceName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_replace_filter);
        ButterKnife.bind(this);
        setStatusBar(R.id.view_status_bar);
        init();
        initView();
    }

    private void init() {
        ScreenWidth = DisplayUtil.getScreenWidth(this);
        checkBox1.setClickable(false);
        checkBox2.setClickable(false);
        checkBox3.setClickable(false);
        checkBox4.setClickable(false);
    }


    private void initView() {
        try {
            deviceName = getIntent().getStringExtra("deviceName");
            mMac = getIntent().getStringExtra("mac");
            ArrayList<DevFilterInfoBean> filterState = (ArrayList<DevFilterInfoBean>) getIntent().getSerializableExtra("filterState");
            if (filterState == null || filterState.size() != 4) {
                throw new Exception("数据异常");
            }
            checkBox1.setChecked(false);
            checkBox2.setChecked(false);
            checkBox3.setChecked(false);
            checkBox4.setChecked(false);
            mA1FilterDay = filterState.get(0).getDay();
            mA2FilterDay = filterState.get(1).getDay();
            mA3FilterDay = filterState.get(2).getDay();
            mA4FilterDay = filterState.get(3).getDay();
            textFilter1.setText("A1滤芯还可用" + mA1FilterDay + "天");
            textFilter2.setText("A2滤芯还可用" + mA2FilterDay + "天");
            textFilter3.setText("A3滤芯还可用" + mA3FilterDay + "天");
            textFilter4.setText("A4滤芯还可用" + mA4FilterDay + "天");
            if (deviceName == null) {
                deviceName = "暂无名称";
            }
            tabLayoutView.setTabs(new String[]{deviceName});
        } catch (Exception e) {
            nodataImage.setImageResource(R.drawable.default_error_image);
            nodataText.setText(getString(R.string.error));
            layoutContent.setVisibility(View.GONE);
            layoutNodata.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.layout_left)
    public void layoutLeft() {
        finish();
    }

    @OnClick({R.id.layout_filter1, R.id.layout_filter2, R.id.layout_filter3, R.id.layout_filter4, R.id.btn_replace})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_filter1:
                if (mA1FilterDay > minReplaceDay) {
                    Toast("低于10天的滤芯才可重置");
                    return;
                }
                checkBox1.setChecked(checkBox1.isChecked() ? false : true, true);
                checkBox2.setChecked(false);
                checkBox3.setChecked(false);
                checkBox4.setChecked(false);
                break;
            case R.id.layout_filter2:
                if (mA2FilterDay > minReplaceDay) {
                    Toast("低于10天的滤芯才可重置");
                    return;
                }
                checkBox2.setChecked(checkBox2.isChecked() ? false : true, true);
                checkBox1.setChecked(false);
                checkBox3.setChecked(false);
                checkBox4.setChecked(false);
                break;
            case R.id.layout_filter3:
                if (mA3FilterDay > minReplaceDay) {
                    Toast("低于10天的滤芯才可重置");
                    return;
                }
                checkBox3.setChecked(checkBox3.isChecked() ? false : true, true);
                checkBox1.setChecked(false);
                checkBox2.setChecked(false);
                checkBox4.setChecked(false);
                break;
            case R.id.layout_filter4:
                if (mA3FilterDay > minReplaceDay) {
                    Toast("低于10天的滤芯才可重置");
                    return;
                }
                checkBox4.setChecked(checkBox4.isChecked() ? false : true, true);
                checkBox1.setChecked(false);
                checkBox2.setChecked(false);
                checkBox3.setChecked(false);
                break;
            case R.id.btn_replace:
                if (!checkBox1.isChecked() && !checkBox2.isChecked() && !checkBox3.isChecked() && !checkBox4.isChecked()) {
                    Toast("请选择要复位的滤芯");
                } else {
                    new AlertDialog.Builder(this).setTitle("提示").setMessage("你确定要重置滤芯吗?")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    if (checkBox1.isChecked()) {
                                        mLevel = 1;
                                    }
                                    if (checkBox2.isChecked()) {
                                        mLevel = 2;
                                    }
                                    if (checkBox3.isChecked()) {
                                        mLevel = 3;
                                    }
                                    if (checkBox4.isChecked()) {
                                        mLevel = 4;
                                    }
                                    if (mMac != null) {
                                        checkBox1.setChecked(false);
                                        checkBox2.setChecked(false);
                                        checkBox3.setChecked(false);
                                        checkBox4.setChecked(false);
                                        new DeviceResetFilterControl(ReplaceFilterActivity.this, mMac, mLevel, false);
                                    } else {
                                        Toast(getString(R.string.error));
                                    }
                                }
                            })
                            .setNegativeButton("取消", null)
                            .show();
                }
                break;
        }
    }

    private void setTextColor() {
        if (mA1FilterDay == 0) {
            textFilter1.setTextColor(0xffcf0d16);
        } else if (mA1FilterDay > 0 && mA1FilterDay < 5) {
            textFilter1.setTextColor(0xfffe8340);
        } else {
            textFilter1.setTextColor(0xff3688ff);
        }
        if (mA2FilterDay == 0) {
            textFilter2.setTextColor(0xffcf0d16);
        } else if (mA2FilterDay > 0 && mA2FilterDay < 5) {
            textFilter2.setTextColor(0xfffe8340);
        } else {
            textFilter2.setTextColor(0xff3688ff);
        }
        if (mA3FilterDay == 0) {
            textFilter3.setTextColor(0xffcf0d16);
        } else if (mA3FilterDay > 0 && mA3FilterDay < 5) {
            textFilter3.setTextColor(0xfffe8340);
        } else {
            textFilter3.setTextColor(0xff3688ff);
        }
        if (mA4FilterDay == 0) {
            textFilter4.setTextColor(0xffcf0d16);
        } else if (mA4FilterDay > 0 && mA4FilterDay < 5) {
            textFilter4.setTextColor(0xfffe8340);
        } else {
            textFilter4.setTextColor(0xff3688ff);
        }
    }


    private void showLoading() {
        layoutLoading.setVisibility(View.VISIBLE);
        layoutContent.setVisibility(View.GONE);
        layoutNodata.setVisibility(View.GONE);
    }

    private void showNoData() {
        layoutNodata.setVisibility(View.VISIBLE);
        layoutLoading.setVisibility(View.GONE);
        layoutContent.setVisibility(View.GONE);
    }

    private void hideEmptyLayout() {
        layoutContent.setVisibility(View.VISIBLE);
        layoutNodata.setVisibility(View.GONE);
        layoutLoading.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


}
